/*
 *	Copyright © 2013 Changsha Shishuo Network Technology Co., Ltd. All rights reserved.
 *	长沙市师说网络科技有限公司 版权所有
 *	http://www.shishuo.com
 */

package com.hlingsoft.kkh.action;

import com.hlingsoft.kkh.dao.AdminResumeDao;
import com.hlingsoft.kkh.entity.Resume;
import com.hlingsoft.kkh.entity.vo.ResumeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 简历
 *
 * @author Herbert
 */
@Controller
@RequestMapping("/resume")
public class ResumeAction extends BaseAction {
    @Autowired
    private AdminResumeDao resumeDao;

    private static final int rows = 999;

    /**
     * 简历添加
     * @param resumeVo
     * @param request
     * @return
     */
    @RequestMapping(value = "/add.json")
    @ResponseBody
    public Map<String, Object> add(@RequestBody ResumeVo resumeVo,
                                   HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Resume resume = new Resume();
            int count = resumeDao.getAllResumeListCount(resumeVo.getAppUserId());
            resume.setAppUserId(resumeVo.getAppUserId());
            resume.setPhone(resumeVo.getPhone());
            resume.setName(resumeVo.getName());
            if (count == 0) {
                resume.setIsDefault(1);
            } else {
                resume.setIsDefault(0);
            }

            resume.setGender(resumeVo.getGender());
            resume.setDescription(resumeVo.getDescription());
            resume.setUniversity(resumeVo.getUniversity());
            resume.setResumeName(resumeVo.getResumeName());
            Date date = new Date();
            resume.setCreateTime(date);
            resume.setUpdateTime(date);

            resumeDao.add(resume);
            resultMap.put("result", true);

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }

        return resultMap;

    }


    /**
     * 简历列表
     * @param userId
     * @param pageNum
     * @param request
     * @return
     */
    @RequestMapping(value = "/list.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> list(
            @RequestParam(value = "userId") long userId,
            @RequestParam(value = "pageNum", defaultValue = "0") int pageNum,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            List<Resume> resumeList = resumeDao.getAllResumeList(userId, pageNum * rows, rows);
            resultMap.put("resumeList", resumeList);
            resultMap.put("result", true);

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }

        return resultMap;

    }

    /**
     * 简历详情
     * @param resumeId
     * @param request
     * @return
     */
    @RequestMapping(value = "/detail.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> detail(
            @RequestParam(value = "resumeId") long resumeId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Resume resume = resumeDao.getResumeById(resumeId);

            resultMap.put("resume", resume);
            resultMap.put("result", true);

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }

        return resultMap;

    }

    /**
     * 设置默认简历
     * @param appUserId
     * @param resumeId
     * @param request
     * @return
     */
    @RequestMapping(value = "/setDefault.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> setDefault(
            @RequestParam(value = "appUserId") long appUserId,
            @RequestParam(value = "resumeId") long resumeId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            resumeDao.setUnDefaultResumeByUserId(appUserId);
            resumeDao.setDefaultResume(resumeId);
            resultMap.put("result", true);

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }

        return resultMap;

    }

    /**
     * 更新简历
     * @param resumeVo
     * @param request
     * @return
     */
    @RequestMapping(value = "/update.json")
    @ResponseBody
    public Map<String, Object> update(@RequestBody ResumeVo resumeVo,
                                      HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {

            Resume resume = new Resume();
            resume.setId(resumeVo.getId());
            resume.setAppUserId(resumeVo.getAppUserId());
            resume.setGender(resumeVo.getGender());
            resume.setUniversity(resumeVo.getUniversity());
            resume.setDescription(resumeVo.getDescription());
            resume.setName(resumeVo.getName());
            resume.setPhone(resumeVo.getPhone());
            Date date = new Date();
            resume.setUpdateTime(date);
            resume.setStatus(0);
            resume.setResumeName(resumeVo.getResumeName());
            if (resumeVo.getIsDefault() == 1) {
                resumeDao.setUnDefaultResumeByUserId(resumeVo.getAppUserId());
            }
            resume.setIsDefault(resumeVo.getIsDefault());
            resumeDao.updateResume(resume);
            resultMap.put("resume", resume);
            resultMap.put("result", true);

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }

        return resultMap;

    }

    /**
     * 简历删除
     * @param resumeId
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> delete(
            @RequestParam(value = "resumeId") long resumeId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            resumeDao.delete(resumeId);
            resultMap.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }

        return resultMap;

    }
}
